package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.HospitalizationMapper;
import com.kkb.mapper.PricePeopleMapper;
import com.kkb.pojo.Hospitalization;
import com.kkb.pojo.PricePeople;
import com.kkb.pojo.PricePeopleExample;
import com.kkb.vo.PricePeopleQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PricePeopleService {
    @Resource
    PricePeopleMapper mapper;

    @Resource
    HospitalizationMapper hospitalizationMapper;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int add(PricePeople pricePeople) {
        if (mapper.insertSelective(pricePeople)>0){
            Hospitalization hospitalization = hospitalizationMapper.selectByPatientId(pricePeople.getBehId());
            hospitalization.setState(0);
            return hospitalizationMapper.updateByPrimaryKeySelective(hospitalization);
        }
        return 0;
    }

    public List queryByPatientId(int id) {
        PricePeopleExample example = new PricePeopleExample();
        PricePeopleExample.Criteria criteria = example.createCriteria();

        criteria.andBehIdEqualTo(id);

        return mapper.selectByExample(example);
    }

    public PageInfo queryAll(int index, int size, PricePeopleQueryVO vo) {
        PricePeopleExample example = new PricePeopleExample();
        PricePeopleExample.Criteria criteria = example.createCriteria();
        if (vo.getPeopleId() != null && !vo.getPeopleId().trim().equals(""))
            criteria.andBehIdEqualTo(Integer.parseInt(vo.getPeopleId().trim()));
        if (vo.getPriceId() != null && !vo.getPriceId().trim().equals(""))
            criteria.andChapIdEqualTo(Integer.parseInt(vo.getPriceId().trim()));

        PageHelper.startPage(index, size);
        List<PricePeople> result = mapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(result);
        return pageInfo;
    }

    public PageInfo<PricePeople> selectByHosRegisterId(Integer index, Integer size, Integer hosId) {
        PricePeopleExample example = new PricePeopleExample();
        PricePeopleExample.Criteria criteria = example.createCriteria();
        criteria.andBehIdEqualTo(hosId);
        PageHelper.startPage(index,size);
        List<PricePeople> pricePeople = mapper.selectByExample(example);
        return new PageInfo<>(pricePeople);
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteByHosRegisterId(Integer id){
        PricePeopleExample pricePeopleExample = new PricePeopleExample();
        int i = 0;
        if (id!=null) {
            pricePeopleExample.createCriteria().andBehIdEqualTo(id);
            Hospitalization hospitalization = hospitalizationMapper.selectByPatientId(id);
            if (hospitalization!=null){
                hospitalization.setState(1);
                i = hospitalizationMapper.updateByPrimaryKeySelective(hospitalization);
            }
        }
        int j = mapper.deleteByExample(pricePeopleExample);
        return j+i;
    }
}
